#include "GlobalHead.h"

void func()
{
    char msg[1024] = {0};
    while(1)
    {
        scanf("%s",msg);
        if(strcmp(msg,"quit") == 0)
            _exit(100);
        printf("%s\n",msg);
    }
}

int main()
{
    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork");
        return -1;
    }

    static int a = 1; //不管是什么变量,全局也好,fork()之后都是各用各的


    //子进程
    if(pid == 0)
    {
        //a++;
        printf("这是一条子进程,pid:%d ppid:%d a = %d\n",getpid(),getppid(),a);
        //sleep(1);
        //printf("子进程结束!\n");
        func();
        return 2;
    }
    else
    {
        //sleep(1);
        printf("这是一条父进程,pid:%d ChildID:%d a = %d\n",getpid(),pid,a);
        int result = 0;
        //阻塞性函数:会阻塞进程的函数
        //wait(&result);
        //waitpid(pid,&result,WNOHANG);
        //waitpid(pid,&result,WUNTRACED);
        waitpid(pid,&result,0);

        //WEXITSTATUS():进程的返回值存在状态值的高8位
        printf("子进程已经结束，返回值是%d\n",WEXITSTATUS(result));
    }
    return 0;
}